//============================================================================
// Copyright (C) 2010 Brett R. Jones
// All Rights Reserved
//
// You may redistribute and/or modify for non commercial and commercial uses
// provided this copyright notice remains in place and is not modified
//
// This code is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================

package com.roguetreasure.destroytheworld;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.opengl.GLSurfaceView;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Toast;
import android.util.Log;

class DestroyWorldGLSurfaceView extends GLSurfaceView {
	private static final String LOG_TAG = "DestroyWorldGLSurfaceView: ";

	DestroyWorldActivity m_oActivity = null;
	DestroyWorldRenderer m_oRenderer = null;

	private static native void nativeTouchEvent(int iEventType, int iPosX,
			int iPosY);

	public DestroyWorldGLSurfaceView(Context context) {
		super(context);
	}

	public DestroyWorldGLSurfaceView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void Startup(DestroyWorldActivity oActivity) {
		m_oActivity = (DestroyWorldActivity) oActivity;
		// Log.e(LOG_TAG,
		// "DestroyWorldGLSurfaceView::DestroyWorldGLSurfaceView: constructing renderer"
		// );

		if (m_oActivity.m_bSupportsEs2) {
			if (m_oActivity.isProbablyEmulator()) {
				// Avoids crashes on startup with some emulator images.
				this.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
			}
			// Log.d(LOG_TAG, "Supports GLES2");
			this.setEGLContextClientVersion(2);
		} else {
			// Log.d(LOG_TAG, "DOES NOT Support GLES2");
			// Should never be seen in production, since the manifest filters
			// unsupported devices.
			// Toast.makeText(m_oActivity,
			// "This device does not support OpenGL ES 2.0.",
			// Toast.LENGTH_LONG).show();
		}

		m_oRenderer = new DestroyWorldRenderer(oActivity);
		setRenderer(m_oRenderer);
		// Log.e(LOG_TAG,
		// "DestroyWorldGLSurfaceView::DestroyWorldGLSurfaceView: done" );
	}

	public boolean onTouchEvent(final MotionEvent oEvent) {
		int iAction = oEvent.getAction();
		int iPosX = (int) oEvent.getX();
		int iPosY = (int) oEvent.getY();
		// Log.e(LOG_TAG, "Mouse event " + iAction );
		nativeTouchEvent(iAction, iPosX, iPosY);
		return true;
	}

	// ------------------------------------------------------------------------------
	public void SetParentActivity(DestroyWorldActivity oActivity) {
		m_oActivity = oActivity;
		m_oRenderer.SetParentActivity(oActivity);
	}
}

class DestroyWorldRenderer implements GLSurfaceView.Renderer {
	private static native void nativeResize(int w, int h);

	private static native int nativeRender();

	private static native void nativeDone();

	DestroyWorldActivity m_oActivity;
	private static final String LOG_TAG = "DestroyWorldRenderer: ";

	DestroyWorldRenderer(DestroyWorldActivity oActivity) {
		m_oActivity = oActivity;
	}

	// ------------------------------------------------------------------------------
	public void SetParentActivity(DestroyWorldActivity oActivity) {
		m_oActivity = oActivity;
	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
	}

	public void onSurfaceChanged(GL10 gl, int w, int h) {
		// Log.e(LOG_TAG, "onSurfaceChanged: width=" + w + "height=" + h );
		nativeResize(w, h);
	}

	public void onSurfaceDestroyed(GL10 gl) {
		// Log.e(LOG_TAG, "onSurfaceDestroyed:" );
		m_oActivity.Shutdown();
	}

	public void onDrawFrame(GL10 gl) {
		if (null != m_oActivity) {
			// Log.e(LOG_TAG, "render" );
			int iShutdownRequested = nativeRender();
			if (-3 == iShutdownRequested) {
				// Log.e(LOG_TAG, "Shutdown requested" );
				m_oActivity.Shutdown();
			}
		}
	}
}
